/**
 * @package Infinity-Seed
 * @subpackage view
 *
 * @copyright Copyright (C) 2012-2013 Exia.CESI
 * @author Paul MAGNIN, Arthur MERCIER, Jordan RICHET, Cyril SCHUMACHER
 */
package view;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 * Fenêtre contenant les légendes.
 * @author Cyril SCHUMACHER <cyril.schumacher at viacesi.fr>
 */
public class LegendsWindow {
    // <editor-fold defaultstate="collapsed" desc="Attributs.">
    /**
     * Fenêtre.
     */
    protected JFrame frame;
    /**
     * Position de la fenêtre de simulation.
     */
    private Point point = new Point();
    /**
     * Configuration.
     */
    private infinity.seed.Configuration _configuration;
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Méthodes.">
    // <editor-fold defaultstate="collapsed" desc="Constructeur.">
    /**
     * Constructeur.
     */
    public LegendsWindow() {
        // Récupère une instance de la classe Configuration.
        this._configuration = infinity.seed.Configuration.getInstance();
        // Créer la fenêtre.
        this.frame = new JFrame(this._configuration.get("legends.title"));

        // Empêche le client de fermer et de redimensionner la fenêtre.
        this.frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.frame.setResizable(false);
        this.frame.setBackground(Color.decode(_configuration.get("jwindow.background")));
        this.frame.setUndecorated(true);

        // Initialise les composants.
        this._InitializeComponent();

        // Redimensionne selon les composants, positionne et affiche la fenêtre.
        this.frame.pack();
        this.frame.setLocation(425, 385);
        this.frame.setVisible(true);
    }
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Initialise les composants.">
    /**
     * Initialise les composants.
     */
    private void _InitializeComponent() {
        // <editor-fold defaultstate="collapsed" desc="Initialisation.">
        // Utilisation de police personnalisée.
        infinity.seed.CustomFont customFont = new infinity.seed.CustomFont();

        // Lie les événements à la fenêtre.
        LegendsWindowEvent windowEvent = new LegendsWindowEvent();
        this.frame.addMouseListener(windowEvent);
        this.frame.addMouseMotionListener(windowEvent);

        // Ajoute une disposition au contenu principale.
        BorderLayout contentPaneLayout = new BorderLayout();
        this.frame.getContentPane().setLayout(contentPaneLayout);
        this.frame.getRootPane().setBorder(BorderFactory.createLineBorder(Color.black));
        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Barre de titre.">
        // Barre de titre.
        JPanel titlePanel = new JPanel();
        this.frame.getContentPane().add(titlePanel, BorderLayout.PAGE_START);
        titlePanel.setBackground(Color.decode(_configuration.get("jwindow.background")));
        titlePanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JPanel spacePanel = new JPanel();
        spacePanel.setPreferredSize(new Dimension(0, 5));
        titlePanel.add(spacePanel);
        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Contenu de la fenêtre.">
        Dimension caseColor = new Dimension(20, 20);
        JPanel contentPanel = new JPanel();
        BoxLayout contentPanelLayout = new BoxLayout(contentPanel, BoxLayout.Y_AXIS);
        contentPanel.setLayout(contentPanelLayout);
        contentPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 10));
        this.frame.getContentPane().add(contentPanel);

        JPanel seedPanel = new JPanel();
        seedPanel.setLayout(new GridLayout(0, 2));
        contentPanel.add(seedPanel);

        JPanel inSeedColorPanel = new JPanel();
        seedPanel.add(inSeedColorPanel);
        JPanel seedColorPanel = new JPanel();
        seedColorPanel.setBackground(Color.decode(_configuration.get("cell.seed")));
        seedColorPanel.setPreferredSize(caseColor);
        inSeedColorPanel.add(seedColorPanel);
        JLabel seedLabel = new JLabel(this._configuration.get("seedLabel.text"));
        seedLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        seedPanel.add(seedLabel);

        JPanel bushPanel = new JPanel();
        bushPanel.setLayout(new GridLayout(0, 2));
        contentPanel.add(bushPanel);

        JPanel inBushColorPanel = new JPanel();
        bushPanel.add(inBushColorPanel);
        JPanel bushColorPanel = new JPanel();
        bushColorPanel.setBackground(Color.decode(_configuration.get("cell.bush")));
        bushColorPanel.setPreferredSize(caseColor);
        inBushColorPanel.add(bushColorPanel);
        JLabel bushLabel = new JLabel(this._configuration.get("bushLabel.text"));
        bushLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        bushPanel.add(bushLabel);

        JPanel treePanel = new JPanel();
        treePanel.setLayout(new GridLayout(0, 2));
        contentPanel.add(treePanel);

        JPanel inTreeColorPanel = new JPanel();
        treePanel.add(inTreeColorPanel);
        JPanel treeColorPanel = new JPanel();
        treeColorPanel.setBackground(Color.decode(_configuration.get("cell.tree")));
        treeColorPanel.setPreferredSize(caseColor);
        inTreeColorPanel.add(treeColorPanel);
        JLabel treeLabel = new JLabel(this._configuration.get("treeLabel.text"));
        treeLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        treePanel.add(treeLabel);

        JPanel infiredPanel = new JPanel();
        infiredPanel.setLayout(new GridLayout(0, 2));
        contentPanel.add(infiredPanel);

        JPanel infiredColorPanel = new JPanel();
        infiredPanel.add(infiredColorPanel);
        JPanel inInfiredColorPanel = new JPanel();
        inInfiredColorPanel.setBackground(Color.decode(_configuration.get("cell.infired")));
        inInfiredColorPanel.setPreferredSize(caseColor);
        infiredColorPanel.add(inInfiredColorPanel);
        JLabel infiredLabel = new JLabel(this._configuration.get("infiredLabel.text"));
        infiredLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        infiredPanel.add(infiredLabel);

        JPanel infectedPanel = new JPanel();
        infectedPanel.setLayout(new GridLayout(0, 2));
        contentPanel.add(infectedPanel);

        JPanel infectedColorPanel = new JPanel();
        infectedPanel.add(infectedColorPanel);
        JPanel inInfectedColorPanel = new JPanel();
        inInfectedColorPanel.setBackground(Color.decode(_configuration.get("cell.infected")));
        inInfectedColorPanel.setPreferredSize(caseColor);
        infectedColorPanel.add(inInfectedColorPanel);
        JLabel infectedLabel = new JLabel(this._configuration.get("infectedLabel.text"));
        infectedLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        infectedPanel.add(infectedLabel);

        JPanel ashedPanel = new JPanel();
        ashedPanel.setLayout(new GridLayout(0, 2));
        contentPanel.add(ashedPanel);

        JPanel ashedColorPanel = new JPanel();
        ashedPanel.add(ashedColorPanel);
        JPanel inAshedColorPanel = new JPanel();
        inAshedColorPanel.setBackground(Color.decode(_configuration.get("cell.ashed")));
        inAshedColorPanel.setPreferredSize(caseColor);
        ashedColorPanel.add(inAshedColorPanel);
        JLabel ashedLabel = new JLabel(this._configuration.get("ashedLabel.text"));
        ashedLabel.setFont(customFont.getFont("/resources/open-sans/OpenSans-Regular.ttf", 12));
        ashedPanel.add(ashedLabel);
        // </editor-fold>
    }
    // </editor-fold>
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Evénements.">
    // <editor-fold defaultstate="collapsed" desc="Gestion des évènements de la fenêtre.">
    /**
     * Gestion des évènements de la fenêtre.
     */
    private class LegendsWindowEvent extends MouseAdapter {
        /**
         * Recupere la position du curseur.
         * @param e Parametres.
         */
        @Override public void mousePressed(MouseEvent e) {
            point.x = e.getX();
            point.y = e.getY();
        }
        /**
         * Selon la position du curseur, on déplace la fenêtre.
         * @param e Parametres.
         */
        @Override public void mouseDragged(MouseEvent e) {
            JFrame frame = (JFrame) e.getSource();
            Point position = frame.getLocation();
            frame.setLocation(position.x + e.getX() - point.x, position.y + e.getY() - point.y);
        }
    }
    // </editor-fold>
    // </editor-fold>
}
